﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GN.Pay.Application.AdminApp.Dto.Input;
using GN.Pay.Application.AdminApp.Dto.Output;
using GN.Pay.Application.Dto.Input;
using GN.Pay.Core.Repositories;
using Abp.AutoMapper;
using GN.Pay.Core.Entities;

namespace GN.Pay.Application.AdminApp.Impl
{
    /// <summary>
    /// 
    /// </summary>
    public class SystemConfigureAppService : AdminAppService,ISystemConfigureAppService
    {
        private readonly ISystemConfigureRepository repository;

        public SystemConfigureAppService(ISystemConfigureRepository repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public SystemConfigureOutput AddOrModify(SystemConfigureInput input)
        {
            var entity = repository.Get(input.Id);
            if (entity == null)
            {
                entity = input.MapTo<SystemConfigure>();
                entity.OperatorTime = DateTime.Now;
                repository.Insert(entity);
            }
            else
            {
                entity.OperatorTime = DateTime.Now;
                repository.Update(entity);
            }
            return entity.MapTo<SystemConfigureOutput>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public SystemConfigureOutput Get(PrimaryKeyInput<string> input)
        {
            var entity = repository.Get(input.Id);
            if (entity == null)
            {
                return null;
            }
            return entity.MapTo<SystemConfigureOutput>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool Delete(PrimaryKeyInput<string> input)
        {
            repository.Delete(input.Id);
            return repository.SaveChanges() > 0;
        }
    }
}
